package com.ruoyi.hotel.service.impl;

import java.text.DecimalFormat;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hotel.domain.HlMember;
import com.ruoyi.hotel.domain.HlRoomPrice;
import com.ruoyi.hotel.domain.dto.HlRoomDTO;
import com.ruoyi.hotel.domain.pojo.HlRoomPojo;
import com.ruoyi.hotel.domain.vo.MemberRatioVO;
import com.ruoyi.hotel.domain.vo.MemberTypeVO;
import com.ruoyi.hotel.mapper.HlMemberMapper;
import com.ruoyi.hotel.mapper.HlRoomPriceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hotel.mapper.HlRoomMapper;
import com.ruoyi.hotel.domain.HlRoom;
import com.ruoyi.hotel.service.IHlRoomService;

import static com.ruoyi.common.enums.CommissionType.*;

/**
 * 房间列表Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-10
 */
@Service
public class HlRoomServiceImpl implements IHlRoomService 
{

    @Autowired
    private HlRoomMapper hlRoomMapper;

    @Autowired
    private HlRoomPriceMapper hlRoomPriceMapper;

    @Autowired
    private HlMemberMapper hlMemberMapper;







    /**
     * 查询房间列表
     * 
     * @param id 房间列表主键
     * @return 房间列表
     */
    @Override
    public HlRoom selectHlRoomById(Integer id)
    {
        return hlRoomMapper.selectHlRoomById(id);
    }

    /**
     * 查询房间列表列表
     * 
     * @param hlroomPojo 房间列表
     * @return 房间列表
     */
    @Override
    public List<HlRoomDTO> selectHlRoomList(HlRoomPojo hlroomPojo)
    {


        List<HlRoomDTO> hlRoomDTOS = hlRoomMapper.selectHlRoomList(hlroomPojo);
        List<HlMember> hlMembers = hlMemberMapper.selectHlMemberList(new HlMember());

        MemberRatioVO memberRatioVO = new MemberRatioVO();
        for (HlMember member:hlMembers){
            if (ORDINARY.equals(member.getMemberType())){
                memberRatioVO.setOrdinaryRatio(member.getDiscount());
            }
            if (SILVER.equals(member.getMemberType())){
                memberRatioVO.setSilverRatio(member.getDiscount());
            }
            if (GOLD.equals(member.getMemberType())){
                memberRatioVO.setGoldRatio(member.getDiscount());
            }
            if (PLATINUM.equals(member.getMemberType())){
                memberRatioVO.setPlatinumRatio(member.getDiscount());
            }
            if (DIAMOND.equals(member.getMemberType())){
                memberRatioVO.setDiamondRatio(member.getDiscount());
            }
        }



        for(HlRoomDTO roomDTO:hlRoomDTOS){

//            还有两个复杂的业务 ，一个是计算用户结账的系统（主要是钟点房计算要算上用户的会员权限），一个是计算员工的提成算法（主要是办理会员的提成）####################

            HlRoomPrice hlRoomPriceZero = hlRoomPriceMapper.selectHlRoomPriceStayForZeroByRoomTypeIdAndNum(roomDTO.getRoomTypeId(),roomDTO.getNum());

            MemberTypeVO memberTypeZeroVO = new MemberTypeVO();
//            这里是钟点计费以及各个会员权限的计算结果价格=========================================
//            普通会员
            String ordinaryZero = "" + (((hlRoomPriceZero.getPrice() * 100) * (memberRatioVO.getOrdinaryRatio() * 100)) / 10000);
            memberTypeZeroVO.setOrdinary("." .equals(String.valueOf(ordinaryZero.charAt(ordinaryZero.length() - 2))) ? ordinaryZero + "0" : ordinaryZero);

//            白银会员
            String silverZero = "" + (((hlRoomPriceZero.getPrice() * 100) * (memberRatioVO.getSilverRatio() * 100)) / 10000);
            memberTypeZeroVO.setSilver("." .equals(String.valueOf(silverZero.charAt(silverZero.length() - 2))) ? silverZero + "0" : silverZero);
//            黄金会员
            String goldZero = "" + (((hlRoomPriceZero.getPrice() * 100) * (memberRatioVO.getGoldRatio() * 100)) / 10000);
            memberTypeZeroVO.setGold("." .equals(String.valueOf(goldZero.charAt(goldZero.length() - 2))) ? goldZero + "0" : goldZero);
//            铂金会员
            String platinumZero = "" + (((hlRoomPriceZero.getPrice() * 100) * (memberRatioVO.getPlatinumRatio() * 100)) / 10000);
            memberTypeZeroVO.setPlatinum("." .equals(String.valueOf(platinumZero.charAt(platinumZero.length() - 2))) ? platinumZero + "0" : platinumZero);
//            钻石会员
            String diamondZero = "" + (((hlRoomPriceZero.getPrice() * 100) * (memberRatioVO.getDiamondRatio() * 100)) / 10000);
            memberTypeZeroVO.setDiamond("." .equals(String.valueOf(diamondZero.charAt(diamondZero.length() - 2))) ? diamondZero + "0" : diamondZero);

//            String zeroPrice = "[  " + ("." .equals(String.valueOf((memberTypeZeroVO.getOrdinary() + "").charAt((memberTypeZeroVO.getOrdinary() + "").length() - 2))) ? memberTypeZeroVO.getOrdinary() + "0" : memberTypeZeroVO.getOrdinary() + "") + "  |  " +
//                    ("." .equals(String.valueOf((memberTypeZeroVO.getSilver() + "").charAt((memberTypeZeroVO.getSilver() + "").length() - 2))) ? memberTypeZeroVO.getSilver() + "0" : memberTypeZeroVO.getSilver() + "") + "  |  " +
//                    ("." .equals(String.valueOf((memberTypeZeroVO.getGold() + "").charAt((memberTypeZeroVO.getGold() + "").length() - 2))) ? memberTypeZeroVO.getGold() + "0" : memberTypeZeroVO.getGold() + "") + "  |  " +
//                    ("." .equals(String.valueOf((memberTypeZeroVO.getPlatinum() + "").charAt((memberTypeZeroVO.getPlatinum() + "").length() - 2))) ? memberTypeZeroVO.getPlatinum() + "0" : memberTypeZeroVO.getPlatinum() + "") + "  |  " +
//                    ("." .equals(String.valueOf((memberTypeZeroVO.getDiamond() + "").charAt((memberTypeZeroVO.getDiamond() + "").length() - 2))) ? memberTypeZeroVO.getDiamond() + "0" : memberTypeZeroVO.getDiamond() + "") + "  ]";


//            将值放到容器中
            roomDTO.setStayTypeIsZeroVO(memberTypeZeroVO);

            HlRoomPrice hlRoomPriceOne = hlRoomPriceMapper.selectHlRoomPriceStayForOneByRoomTypeIdAndNum(roomDTO.getRoomTypeId(),roomDTO.getNum());

            MemberTypeVO memberTypeOneVO = new MemberTypeVO();
//            这里是夜宿计费以及各个会员权限的计算结果价格=========================================
//            普通会员
            String ordinaryOne = "" + (((hlRoomPriceOne.getPrice() * 100) * (memberRatioVO.getOrdinaryRatio() * 100)) / 10000);
            memberTypeOneVO.setOrdinary("." .equals(String.valueOf(ordinaryOne.charAt(ordinaryOne.length() - 2))) ? ordinaryOne + "0" : ordinaryOne);
//            白银会员
            String silverOne = "" + (((hlRoomPriceOne.getPrice() * 100) * (memberRatioVO.getSilverRatio() * 100)) / 10000);
            memberTypeOneVO.setSilver("." .equals(String.valueOf(silverOne.charAt(silverOne.length() - 2))) ? silverOne + "0" : silverOne);
//            黄金会员
            String goldOne = "" + (((hlRoomPriceOne.getPrice() * 100) * (memberRatioVO.getGoldRatio() * 100)) / 10000);
            memberTypeOneVO.setGold("." .equals(String.valueOf(goldOne.charAt(goldOne.length() - 2))) ? goldOne + "0" : goldOne);
//            铂金会员
            String platinumOne = "" + (((hlRoomPriceOne.getPrice() * 100) * (memberRatioVO.getPlatinumRatio() * 100)) / 10000);
            memberTypeOneVO.setPlatinum("." .equals(String.valueOf(platinumOne.charAt(platinumOne.length() - 2))) ? platinumOne + "0" : platinumOne);
//            钻石会员
            String diamondOne = "" + (((hlRoomPriceOne.getPrice() * 100) * (memberRatioVO.getDiamondRatio() * 100)) / 10000);
            memberTypeOneVO.setDiamond("." .equals(String.valueOf(diamondOne.charAt(diamondOne.length() - 2))) ? diamondOne + "0" : diamondOne);

//            String OnePrice = "[  " + ("." .equals(String.valueOf((memberTypeOneVO.getOrdinary() + "").charAt((memberTypeOneVO.getOrdinary() + "").length() - 2))) ? memberTypeOneVO.getOrdinary() + "0" : memberTypeOneVO.getOrdinary() + "") + "  |  " +
//                    ("." .equals(String.valueOf((memberTypeOneVO.getSilver() + "").charAt((memberTypeOneVO.getSilver() + "").length() - 2))) ? memberTypeOneVO.getSilver() + "0" : memberTypeOneVO.getSilver() + "") + "  |  " +
//                    ("." .equals(String.valueOf((memberTypeOneVO.getGold() + "").charAt((memberTypeOneVO.getGold() + "").length() - 2))) ? memberTypeOneVO.getGold() + "0" : memberTypeOneVO.getGold() + "") + "  |  " +
//                    ("." .equals(String.valueOf((memberTypeOneVO.getPlatinum() + "").charAt((memberTypeOneVO.getPlatinum() + "").length() - 2))) ? memberTypeOneVO.getPlatinum() + "0" : memberTypeOneVO.getPlatinum() + "") + "  |  " +
//                    ("." .equals(String.valueOf((memberTypeOneVO.getDiamond() + "").charAt((memberTypeOneVO.getDiamond() + "").length() - 2))) ? memberTypeOneVO.getDiamond() + "0" : memberTypeOneVO.getDiamond() + "") + "  ]";


//            将值放到容器中
            roomDTO.setStayTypeIsOneVO(memberTypeOneVO);

//            这里就很离谱，直接roomDTO.getStayType().setZero(hlRoomPriceZero.getPrice());有值
//            接着再进行roomDTO.getStayType().setOne(hlRoomPriceOne.getPrice()); 是null空指针
//            不是顺序问题，也不知道是不是idea的问题
//            另外，startPage();这个分页的工具也有bug，当sql多表三表数据或者数据内容过多时，
//            在前端每一页原本10条数据只显示4条，后端打印发现也是只有4条一页的数据，总结就是他这个方法有BUG，应该是逻辑问题或者变量不严谨的问题


        }

        return hlRoomDTOS;
    }

    /**
     * 新增房间列表
     * 
     * @param hlRoom 房间列表
     * @return 结果
     */
    @Override
    public int insertHlRoom(HlRoom hlRoom)
    {
        hlRoom.setCreateTime(DateUtils.getNowDate());
        return hlRoomMapper.insertHlRoom(hlRoom);
    }

    /**
     * 修改房间列表
     * 
     * @param hlRoom 房间列表
     * @return 结果
     */
    @Override
    public int updateHlRoom(HlRoom hlRoom)
    {
        hlRoom.setUpdateTime(DateUtils.getNowDate());
        return hlRoomMapper.updateHlRoom(hlRoom);
    }

    /**
     * 批量删除房间列表
     * 
     * @param ids 需要删除的房间列表主键
     * @return 结果
     */
    @Override
    public int deleteHlRoomByIds(Integer[] ids)
    {
        return hlRoomMapper.deleteHlRoomByIds(ids);
    }

    /**
     * 删除房间列表信息
     * 
     * @param id 房间列表主键
     * @return 结果
     */
    @Override
    public int deleteHlRoomById(Integer id)
    {
        return hlRoomMapper.deleteHlRoomById(id);
    }
}
